package com.ziyun.pop.modules.mercharReport.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.e.mercharReport.PayStatusEnum;
import com.ziyun.pop.common.e.mercharReport.PaymentEnum;
import com.ziyun.pop.common.e.mercharReport.ReportProductionStatusEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.modules.mercharReport.dao.MercharOrderInfoDao;
import com.ziyun.pop.modules.mercharReport.entity.OrderInfoEntity;
import com.ziyun.pop.modules.mercharReport.entity.ReportOrderInfoVO;
import com.ziyun.pop.modules.mercharReport.entity.TwbReportOrderInfoVO;
import com.ziyun.pop.modules.mercharReport.service.MercharOrderInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Service("mercharOrderInfoService")
public class MercharOrderInfoServiceImpl extends ServiceImpl<MercharOrderInfoDao, OrderInfoEntity> implements MercharOrderInfoService {
    @Autowired
    private MercharOrderInfoDao mercharOrderInfoDao;

    @Override
    public R list(Map<String, Object> params) {
        Page<ReportOrderInfoVO> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<ReportOrderInfoVO> list = mercharOrderInfoDao.list(page, params);
        //处理规格属性
        page.setRecords(this.changeAddon(list));
        PageUtils pageUtils = new PageUtils(page);
        // 存放枚举类到userdata
        Map<String, Object> mapEnum = new HashMap<>();
        List<ReportOrderInfoVO> listForTotal = mercharOrderInfoDao.listAll(params);
        //应收总额
        BigDecimal totalMoney = listForTotal.stream().map(ReportOrderInfoVO::getPayableAmount).reduce(new BigDecimal("0"), BigDecimal::add);
        //实收总额
        BigDecimal payedMoney = listForTotal.stream().map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //支付宝总额
        BigDecimal alipayTatal = listForTotal.stream().filter(v -> "支付宝".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //微信总额
        BigDecimal weixinTatal = listForTotal.stream().filter(v -> "微信".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //现金总额
        BigDecimal cashTatal = listForTotal.stream().filter(v -> "现金".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //银联总额
        BigDecimal unTatal = listForTotal.stream().filter(v -> "银联".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //现金支付总额
        BigDecimal moneyTatal = listForTotal.stream().filter(v -> "现金支付".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);
        //扫码支付总额
        BigDecimal sweepcodeTatal = listForTotal.stream().filter(v -> "扫码支付".equals(v.getPayment())).map(ReportOrderInfoVO::getPayed).reduce(new BigDecimal("0"), BigDecimal::add);


        mapEnum.put("productionStatusList", JsonUtils.enumToList(ReportProductionStatusEnum.class)); //门店订单状态
        mapEnum.put("paymentList", JsonUtils.enumToList(PaymentEnum.class)); //支付方式集合
        mapEnum.put("PayStatusEnum", JsonUtils.enumToList(PayStatusEnum.class)); //付款状态
        mapEnum.put("usernames", this.baseMapper.getUsername((Long) params.get("sellerId")));//业务员集合
        mapEnum.put("totalMoney", totalMoney);
        mapEnum.put("payedMoney", payedMoney);
        mapEnum.put("alipayTatal", alipayTatal);
        mapEnum.put("weixinTatal", weixinTatal);
        mapEnum.put("cashTatal", cashTatal);
        mapEnum.put("unTatal", unTatal);
        mapEnum.put("moneyTatal", moneyTatal);
        mapEnum.put("sweepcodeTatal", sweepcodeTatal);
        mapEnum.put("totalCount", listForTotal.size());
        return R.ok().put("page", pageUtils).put("userData", mapEnum);
    }

    @Override
    public List<ReportOrderInfoVO> exportExcel(Map<String, Object> params) {
        //处理规格属性
        return this.changeAddon(mercharOrderInfoDao.list(params));
    }

    //处理规格属性
    private List<ReportOrderInfoVO> changeAddon(List<ReportOrderInfoVO> list) {
        for (ReportOrderInfoVO reportOrderInfo : list) {
            if (StringUtils.isNotBlank(reportOrderInfo.getAddon()) && reportOrderInfo.getAddon().contains("[{")) {
                String str=reportOrderInfo.getAddon().substring(reportOrderInfo.getAddon().indexOf("[{"),reportOrderInfo.getAddon().lastIndexOf("]")+1);
                JSONArray jsonArray = JSONArray.fromObject(str);
                String addon="";
                for (Object o : jsonArray) {
                    JSONObject basicParam = (JSONObject) ((JSONObject) o).get("basicParam");
                    Iterator<String> it = basicParam.keys();
                    while(it.hasNext()){
                        String key = it.next();
                        addon+=basicParam.getString(key)+" ";
                    }
                }
                reportOrderInfo.setAddon(reportOrderInfo.getAddon().substring(0,reportOrderInfo.getAddon().indexOf("[{"))+addon+reportOrderInfo.getAddon().substring(reportOrderInfo.getAddon().lastIndexOf("]")+1));
            }else if(StringUtils.isNotBlank(reportOrderInfo.getAddon())){
                reportOrderInfo.setAddon(reportOrderInfo.getAddon().replaceAll("\\{","").replaceAll("\\}","").replaceAll("\"",""));
            }
        }
        return list;
    }

}

